import {createStore, applyMiddleware} from 'redux';
import thunk from 'redux-thunk';
import reducers from '../reducers/index';
import { persistStore, persistCombineReducers } from 'redux-persist';
import storage from 'redux-persist/es/storage';
var cloneDeep = require('lodash.clonedeep');


const logger = store => next => action => {

    // console.group(action.type);
    if (typeof action === 'function')
        console.log('dispatching a function');
    else
        console.log('dispatching', action);
    let result = next(action);
    console.log('next state', store.getState());
    // console.groupEnd();
    return result;
}

const config = {
    key: 'root',
    storage,
    whitelist: ['globalReducers'],  // 持久化白名单
}

//配置store信息
export default function configureStore() {
    let reducerData=cloneDeep(reducers);
    //store的创建和持久化
    let reducer = persistCombineReducers(config, reducerData);
    let store = createStore(reducer, applyMiddleware(thunk, logger));

    let persistor = persistStore(store);
    return {store, persistor};
}
